<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">工号</label>
        <div class="layui-input-block">
            <input type="text" id="siId" name="siId" lay-verify="required" lay-reqtext="工号不能为空" placeholder="请输入工号" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">姓名</label>
        <div class="layui-input-block">
            <input type="text" id="siName" name="siName" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="siSex" value="0" title="男" checked="">
            <input type="radio" name="siSex" value="1" title="女">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">出生日期</label>
        <div class="layui-input-block">
            <input type="text" name="siBirth" id="siBirth" lay-verify="date" placeholder="请选择出生日期" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">部门</label>
        <div class="layui-input-block">
            <select name="dpId" lay-verify="required" id="dpId">
                <option value="">请选择</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">岗位</label>
        <div class="layui-input-block">
            <select name="psId" lay-verify="required" id="psId">
                <option value="">请选择</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">密码</label>
        <div class="layui-input-block">
            <input type="password" name="siPass" id="siPass" lay-verify="required" placeholder="请输入密码" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">权限</label>
        <div class="layui-input-block">
            <select name="siAuth" lay-verify="required" id="siAuth">
                <option value="">请选择</option>
                <option value="0">普通员工</option>
                <option value="1">管理员</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    const showPass = document.getElementById('showPass');
    const passwordInput = document.querySelector('input[type="password"]');

    passwordInput.addEventListener('dblclick', () => {
        if (passwordInput.type === 'password') {
            passwordInput.type = 'text';
        } else {
            passwordInput.type = 'password';
        }
    });


    let dept = []
    let position = []
    var $ = layui.$

    let promiseDept = $.ajax({
        url: "/deptShowAll",
        type: "get",
        data: {},
        dataType: "text",
        contentType: "text/html",
        success: function (data) {
            var data = JSON.parse(data);
            if (data.code == "0") {
                var deptDate = data.data;
                for (var i = 0; i < deptDate.length; i++) {
                    dept.push({
                        "dpId": deptDate[i].dpId,
                        "dpName": deptDate[i].dpName,
                    })
                }
            }
            //resolve();
        },
        error:function (data){
            //reject(data)
        }
    })

    let promisePosition = $.ajax({
        url: "/positionShowAll",
        type: "get",
        data: {},
        dataType: "text",
        contentType: "text/html",
        success: function (data) {
            var data = JSON.parse(data);
            if (data.code == "0") {
                var positionDate = data.data;
                for (var i = 0; i < positionDate.length; i++) {
                    position.push({
                        "psId": positionDate[i].psId,
                        "psName": positionDate[i].psName,
                    })
                }
            }
        },
        error:function (data){
        }
    })

    function getDeptDate() {
        var $ = layui.$
        return Promise.all([promiseDept,promisePosition]).then(function (results){
        }).catch(function (error) {
            layer.msg('失败，' + error , function () {
            });
        })
    }

    function appendHtml(){
        var $ = layui.$;
        for (const date of position) {
            $('#psId').append("<option value='"+date.psId+"'>"+date.psName+"</option>");
        }
        for (const date of dept) {
            $('#dpId').append("<option value='"+date.dpId+"'>"+date.dpName+"</option>");
        }
    }


    getDeptDate().then(function () {
        layui.use(['form','laydate'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$,
                laydate = layui.laydate;
            laydate.render({
                elem: '#siBirth'
            });
            appendHtml()

            // 更新表单渲染
            form.render('select');

            //监听提交
            form.on('submit(saveBtn)', function (data) {
                console.log("提交了")
                $.ajax({
                    url:'/staffAdd',
                    type:'get',
                    data:{
                        "siId":data.field.siId,
                        "siName":data.field.siName,
                        "siSex":data.field.siSex,
                        "siBirth":data.field.siBirth,
                        "psId":data.field.psId,
                        "dpId":data.field.dpId,
                        "siPass":data.field.siPass,
                        "siAuth":data.field.siAuth,
                    },
                    dataType:'text',
                    contentType:'application/json',
                    success:function (data){
                        var data = JSON.parse(data);
                        if (data.code == "0") {
                            layer.msg('添加成功', function () {
                                window.location = '/page/staff.html';

                            });
                        } else {
                            if (data.description != "null") {
                                layer.msg('添加失败，' + data.description + '', function () {
                                });
                            } else {
                                layer.msg('添加失败，' + data.message + '', function () {
                                });
                            }
                        }
                    }
                })


                return false;
            });

        });
    })
</script>
</body>
</html>
